package com.hanlink.dsp;

import android.app.Application;

import com.blankj.utilcode.util.Utils;
import com.qztech.bluetooth.BluetoothClientManager;
import com.qztech.bluetooth.BluetoothConfig;
import com.qztech.sdk.BaseApplication;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

import org.xutils.DbManager;
import org.xutils.x;

import java.util.Arrays;

import static com.hanlink.dsp.DspSettings.EQ_SETTINGS;
import static com.hanlink.dsp.DspSettings.FRENCY_LENGTH;

/**
 * @author Zhanghb
 * Email: 2573475062@qq.com
 * Date : 2020-03-29 14:44
 */
public class DspContext {
    public static  long BLE_WRITE_DELAY = 100;
    public static  long BLE_WRITE_DELAY_LARGE = 100;


    public static final String DSP_MODEL_DSP125 = "DSP125.4D";
    public static final String DSP_MODEL_DSP600 = "DSP600.4D";
    public static final String DSP_MODEL_PBX_DSP8 = "PBX-DSP8";

    public static final String KEY_PWD = "key_pwd";
    public final static String PASSOWRD = "";

    public static void init(Application application) {
        initDb(application);
        initBluetoothConfig();
        Utils.init(application);
    }

    public static enum DspDevType{
        DSP_UNKNOWN,
        DSP_DSP125_4D,
        DSP_DSP600_4D,
    }

    public static DspDevType getDevType(){
        BluetoothClientManager manager = BluetoothClientManager.getInstance(BtDspApplication.getInstance());
        if (manager.isConnect()){
            try {
                final String name = manager.getConnectedDevice().getName();
                if (name == null)return DspDevType.DSP_UNKNOWN;
                if (name.contains(DSP_MODEL_DSP125) || DSP_MODEL_DSP125.contains(name)){
                    return DspDevType.DSP_DSP125_4D;
                }

                if (name.contains(DSP_MODEL_DSP600) || DSP_MODEL_DSP600.contains(name)){
                    return DspDevType.DSP_DSP600_4D;
                }
            } catch (Exception e) {
                YzDebug.d("Error:getDevType", e);
            }
        }
        return DspDevType.DSP_UNKNOWN;
    }


    private static void initBluetoothConfig() {
        BluetoothConfig.init(
                "0000faa0-0000-1000-8000-00805f9b34fb",
                "0000faa1-0000-1000-8000-00805f9b34fb",
                "0000faa1-0000-1000-8000-00805f9b34fb",
                null,
                DSP_MODEL_PBX_DSP8);
    }

    private static void initDb(Application application) {
        // CrashReport.initCrashReport(getApplicationContext(), "a9e119d2b5", false);
        x.Ext.init(application);
        x.Ext.setDebug(true); // 是否输出debug日志
        BaseApplication.daoConfig = new DbManager.DaoConfig()
                .setDbName("yz_dsp")
                .setDbVersion(10)
                .setDbUpgradeListener((db, oldVersion, newVersion) -> {
                    // db.addColumn(...);
//                     db.dropTable(...);
                    // ...
                });

        try {
            ((BaseApplication) application).setDbManager(x.getDb(BaseApplication.daoConfig));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public static boolean updateByConnectedDevices(String deviceName){
        if (DSP_MODEL_DSP600.equals(deviceName)){
            BLE_WRITE_DELAY = 30;
            BLE_WRITE_DELAY_LARGE = 30;
            EQ_SETTINGS.resetQValue(200F);
            return true;
        }
        return false;
    }

    //
    public static float[] getDefaultQValues() {
        float[] q = new float[FRENCY_LENGTH];
        Arrays.fill(q, SettingConstants.Q);
        return q;
    }

    public static int[] getDefaultGainValues() {
        int[] gains = new int[FRENCY_LENGTH];
        Arrays.fill(gains, SettingConstants.GAIN_MAX);
        return gains;
    }
}
